﻿define(function (require, exports, module) {
    var PDFRect = WebPDF.PDFRect;
    //var CommonTools = WebPDF.Common;
    var RectUtils = WebPDF.RectUtils;
    WebPDF.PDFData.Text = {};

    /**
     * Char information
     * @private
     * @constructor
     */
    var CharInfo = function () {
        this.charRect = new PDFRect(0, 0, 0, 0);
        this.charCode = -1;
    };
    WebPDF.PDFData.Text.CharInfo = CharInfo;
    /**
     * Caret information
     * @constructor
     * @private
     */
    var CaretInfo = function () {
        this.index = -1;
        this.bLeft = true;
    };
    
    WebPDF.PDFData.Text.WritingMode = 
	{
		 TEXT_WRITING_MODE_LRTB :  0,
		 TEXT_WRITING_MODE_RLTB :  1,
		 TEXT_WRITING_MODE_LRBT :  2,
		 TEXT_WRITING_MODE_RLBT :  3
	};
    
    WebPDF.PDFData.Text.CaretInfo = CaretInfo;

    /**
     * @private
     * @constructor
     */
    WebPDF.PDFData.Text.PDFTextObject = function () {
        /**
         * Object index of  all objects, including the original characters as well as implicitly increase spaces.
         * @type {number}
         */
        this.objIndex = 0;
        /**
         * Char index of all the characters, including the implicit increase spaces and line breaks
         * @type {number}
         */
        this.charIndex = 0;
        this.lineIndex = 0;
        /**
         * All character information using Json Array to storage, for example, [left, top, width, height, charcode], must in PDF coordinates.
         * @type {Array.<CharInfo>}
         */
        this.charInfoArray = [];
        /**
         * Current text of a text object, through getCharCodeText to get
         * @type {string}
         */
        this.objText = "";
        /**
         * Length of object text
         * @type {number}
         */
        this.strSize = 0;
        this.bTrueText = false;
        this.textPageMatrix = null;
        /**
         * 用于缓存整个object的矩形区域
         * @type {WebPDF.PDFRect}
         * @private
         */
        var _cachedAllObjRect = null;

        /**
         * 初始化
         */
        this.init = function () {
            this.objText = this.getAllCharCodeText();
            this.strSize = this.objText.length;//CommonTools.getStringLength(this.csText);
            this.bTrueText = true;
        };

        /**
         * 获取对象数量
         * @returns {Array.length|*}
         */
        this.getSize = function () {
            return this.charInfoArray.length;
        };

        /**
         * 获取文本对象的文本长度
         * @returns {number} 文本对象的文本长度
         */
        this.getStrSize = function () {
            return this.strSize;
        };
        /**
         * 通过字符索引获取对象索引.
         * @param {Number} charIndex 字符索引
         * @returns {number} 对应的对象索引
         */
        this.getObjectIndex = function (charIndex) {
            var itemSize = this.charInfoArray.length;
            if (charIndex < 0 || charIndex >= this.strSize || itemSize == 0)
                return 0;
            var charCount = 0;
            for (var index = 0; index < itemSize; index++) {
                var text = this.getCharCodeText(index, 1);
                charCount += text.length;
                if (charIndex + 1 <= charCount) {
                    return index;
                }
            }
            return 0;
        };

        /**
         * 获取整个文本的矩形区域
         * @returns {WebPDF.PDFRect} 整个文本的矩形区域
         */
        this.getObjectAllTextRect = function () {
            return this.getObjectTextRect(0, this.charInfoArray.length);
        };

        /**
         * 获取指定范围内的文本的矩形区域
         * @param startItemIndex 起始项索引
         * @param charCount 数量
         * @returns {WebPDF.PDFRect} 指定范围内的文本的矩形区域
         */
        this.getObjectTextRect = function (startItemIndex, itemCount) {
            var charInfoLength = this.charInfoArray.length;
            var textRect = null;
            if (itemCount <= 0 || charInfoLength == 0 || startItemIndex + itemCount - 1 < 0
                || startItemIndex + itemCount - 1 >= charInfoLength) {
                textRect = new PDFRect(0, 0, 0, 0);
                return textRect;
            }
            textRect = RectUtils.clone(this.charInfoArray[startItemIndex].charRect);
            for (var i = startItemIndex + 1; i < startItemIndex + itemCount; i++) {
                var tmpRect = this.charInfoArray[i].charRect;
                RectUtils.union(textRect, tmpRect);
            }
            return textRect;
        };
        /**
         * 获取整个object的矩形区域
         * @param bClone 是否克隆
         * @returns {WebPDF.PDFRect} 整个object的矩形区域
         */
        this.getAllObjectPDFRect = function (bClone) {
            if (!_cachedAllObjRect) {
                _cachedAllObjRect = this.getObjectPDFRect(0, this.charInfoArray.length);
            }
            if (bClone) {
                return RectUtils.clone(_cachedAllObjRect);
            }
            else {
                return _cachedAllObjRect;
            }
        };
        /**
         * 获取指定范围的文本所占的PDF矩形区域
         * @param startItemIndex 起始索引
         * @param itemCount 数量
         * @returns {WebPDF.PDFRect} 指定范围的文本所占的PDF矩形区域
         */
        this.getObjectPDFRect = function (startItemIndex, itemCount) {
            var charInfoLength = this.charInfoArray.length;
            if (startItemIndex < 0 || startItemIndex >= charInfoLength || charInfoLength == 0) {
                return new PDFRect(0, 0, 0, 0);
            }
            if (startItemIndex + itemCount - 1 >= charInfoLength || itemCount == -1) {
                itemCount = charInfoLength - startItemIndex;
            }
            if (startItemIndex + itemCount - 1 < 0 || startItemIndex + itemCount - 1 >= charInfoLength) {
                return new PDFRect(0, 0, 0, 0);
            }
            var pdfRect = this.getObjectTextRect(startItemIndex, itemCount);
            return pdfRect;
        };

        /**
         * 获取整个对象的文本
         * @returns {String} 整个对象的文本
         */
        this.getAllCharCodeText = function () {
            return this.getCharCodeText(0, this.charInfoArray.length);
        };

        /**
         * 获取指定范围的文本
         * @param startItemIndex 起始索引
         * @param count 数量
         * @returns {String} 指定范围的文本
         */
        this.getCharCodeText = function (startItemIndex, count) {
            var charInfoLength = this.charInfoArray.length;
            if (startItemIndex < 0 || startItemIndex >= charInfoLength)
                return "";
            if (this.bTrueText == true && startItemIndex == 0 && count >= charInfoLength) {
                return this.objText;
            }
            if (startItemIndex + count - 1 >= charInfoLength || count == -1) {
                count = charInfoLength - startItemIndex;
            }

            var text = "";
            var endItemIndex = startItemIndex + count - 1;
            for (var i = startItemIndex; i <= endItemIndex; i++) {
                var charInfo = this.charInfoArray[i];
                var charText = String.fromCharCode(charInfo.charCode);
                text += charText;
            }
            return text;
        };

        /**
         * 根据页面指定的光标位置获取光标落在哪个字符上
         * @param {WebPDF.PDFPoint} point 指定的光标位置
         * @param {CaretInfo} caretInfo 用户接受光标所处的位置信息
         * @returns {boolean} 光标落在某个字符上，返回true，否则返回false
         */
        this.getTextCaretInfo = function (point, caretInfo) {
            var charInfoCount = this.charInfoArray.length;
            if (charInfoCount <= 0) {
                return false;
            }
            caretInfo.index = -1;
            //var caretRect = new PDFRect(0, 0, 0, 0);
            var objectRect = this.getObjectAllTextRect();
            if (!RectUtils.ptInRect(objectRect, point)) {
                return false;
            }
            //判断光标落在哪一个字符上，暂不处理竖排文本
            /* if (isVertWriting)
             {
             /*    for(int i=0; i<nCount; i++){
             PDFRect char_rect = GetObjectTextRect(i,1);
             caret_rect=char_rect;
             if(char_rect.Contains(posx,posy)){
             float n     = fixabs(posy-char_rect.top);
             float height = fixabs(char_rect.bottom-char_rect.top);
             if(n>height/2){//[如果当前点在大部分落在左部,那caret的left和right都取左部(left);否则都取右部(right)]
             caret_rect.top=caret_rect.bottom=char_rect.bottom;
             caret_info.bLeft=FALSE;
             }
             else{
             caret_rect.top =caret_rect.bottom =char_rect.top;
             caret_info.bLeft=TRUE;
             }
             caret_info.caret_rect=caret_rect;
             caret_info.index=i;
             break;
             }
             }
             }
             else {*/
            for (var i = 0; i < charInfoCount; i++) {
                var charRect = this.getObjectTextRect(i, 1);
                if (RectUtils.ptInRect(charRect, point)) {
                    var n = Math.abs(point.x - charRect.left);
                    var width = Math.abs(charRect.right - charRect.left);
                    if (n > width / 2) {
                        //[如果当前点在大部分落在左部,那caret的left和right都取左部(left);否则都取右部(right)]
                        caretInfo.bLeft = false;
                    }
                    else {
                        caretInfo.bLeft = true;
                    }
                    caretInfo.index = i;
                    break;
                }
            }
            //}
            return caretInfo.index != -1;
        };

        /**
         * 根据指定的索引获取光标信息
         * @param itemIndex 指定项索引
         * @param {CaretInfo} caretInfo 用于接收光标信息
         * @param bLeft
         * @returns {boolean} 光标落在某个字符上，返回true，否则返回false
         */
        this.getTextCaretInfoByIndex = function (itemIndex, caretInfo, bLeft) {
            var charInfoCount = this.charInfoArray.length;
            if (charInfoCount <= 0) {
                return false;
            }
            //[光标落在哪一个字符上]
            if (itemIndex < 0 || itemIndex >= charInfoCount)
                return false;
            caretInfo.bLeft = bLeft;
            caretInfo.index = itemIndex;
            return true;
        };
    };

    return WebPDF.PDFData.Text.PDFTextObject;
})
;